package com.bianmaba.spring.mvc.configure;

import com.bianmaba.spring.mvc.advice.StrutsJsonResponseBodyAdvice;
import com.bianmaba.spring.mvc.method.support.EntityParamListMethodArgumentResolver;
import com.bianmaba.spring.mvc.method.support.EntityParamMethodArgumentResolver;
import com.bianmaba.spring.mvc.method.support.MapParamMethodArgumentResolver;
import com.bianmaba.spring.mvc.method.support.StrutsJsonHandlerMethodReturnValueHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Import(StrutsJsonHandlerMethodReturnValueHandler.class)
public class ExtendedWebMvcAutoConfiguration implements WebMvcConfigurer {
    private static final Log LOG = LogFactory.getLog(ExtendedWebMvcAutoConfiguration.class);
    @Autowired
    private StrutsJsonHandlerMethodReturnValueHandler strutsJsonHandlerMethodReturnValueHandler;

    public ExtendedWebMvcAutoConfiguration() {
        super();
        LOG.info("加载MVC扩展自动化配置。");
    }

    @Bean
    public StrutsJsonResponseBodyAdvice strutsJsonResponseBodyAdvice() {
        return new StrutsJsonResponseBodyAdvice();
    }


    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
        returnValueHandlers.add(strutsJsonHandlerMethodReturnValueHandler);
    }

    @Bean
    public EntityParamListMethodArgumentResolver entityParamListMethodArgumentResolver() {
        return new EntityParamListMethodArgumentResolver();
    }

    @Bean
    public EntityParamMethodArgumentResolver entityParamMethodArgumentResolver() {
        return new EntityParamMethodArgumentResolver();
    }


    @Bean
    public MapParamMethodArgumentResolver mapParamMethodArgumentResolver() {
        return new MapParamMethodArgumentResolver();
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(entityParamListMethodArgumentResolver());
        argumentResolvers.add(entityParamMethodArgumentResolver());
        argumentResolvers.add(mapParamMethodArgumentResolver());
    }
}
